package com.cc.screen;


import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import org.bytedeco.javacv.FFmpegFrameGrabber;

import java.io.File;
import java.util.Date;

/**
 * @ClassName：JacobUtils.java
 * @Description： 文字转语音
 **/
public class JacobUtils {

    /**
     * @MonthName：textToSpeech
     * @return：void
     **/
    private static  String audioPath ="E:\\video\\text";
    public static void textToSpeech(String text) {
        ActiveXComponent ax = null;
        try {
            ax = new ActiveXComponent("Sapi.SpVoice");

            // 运行时输出语音内容
            Dispatch spVoice = ax.getObject();
            // 音量 0-100
            ax.setProperty("Volume", new Variant(100));
            // 语音朗读速度 -10 到 +10
            ax.setProperty("Rate", new Variant(-2));
            // 执行朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 构建文件流把生成语音文件
            ax = new ActiveXComponent("Sapi.SpFileStream");
            Dispatch spFileStream = ax.getObject();

            ax = new ActiveXComponent("Sapi.SpAudioFormat");
            Dispatch spAudioFormat = ax.getObject();

            // 设置音频流格式
            Dispatch.put(spAudioFormat, "Type", new Variant(22));
            // 设置文件输出流格式
            Dispatch.putRef(spFileStream, "Format", spAudioFormat);
            // 调用输出 文件流打开方法，创建一个.wav文件
            Dispatch.call(spFileStream, "Open", new Variant(audioPath+".wav"), new Variant(3), new Variant(true));
            // 设置声音对象的音频输出流为输出文件对象
            Dispatch.putRef(spVoice, "AudioOutputStream", spFileStream);
            // 设置音量 0到100
            Dispatch.put(spVoice, "Volume", new Variant(100));
            // 设置朗读速度
            Dispatch.put(spVoice, "Rate", new Variant(-2));
            // 开始朗读
            Dispatch.call(spVoice, "Speak", new Variant(text));

            // 关闭输出文件
            Dispatch.call(spFileStream, "Close");
            Dispatch.putRef(spVoice, "AudioOutputStream", null);

            spAudioFormat.safeRelease();
            spFileStream.safeRelease();
            spVoice.safeRelease();

            ax.safeRelease();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {

        textToSpeech("望着测验魔石碑上面闪亮得甚至有些刺眼的五个大字，少年面无表情，唇角有着一抹自嘲，紧握的手掌，因为大力，而导致略微尖锐的指甲深深的刺进了掌心之中，带来一阵阵钻心的疼痛");
        String path = "E:\\video\\text.wav";

        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(path);
        grabber.start();
        long lengthInTime = grabber.getLengthInTime();
        System.out.println("秒数：" + lengthInTime / 1000 / 1000);
        SubtitleGenerator.generateSRT("望着测验魔石碑上面闪亮得甚至有些刺眼的五个大字，少年面无表情，唇角有着一抹自嘲，紧握的手掌，因为大力，而导致略微尖锐的指甲深深的刺进了掌心之中，带来一阵阵钻心的疼痛","E:\\video\\"+new Date().getTime(),lengthInTime / 1000 / 1000);

    }

    public static void textToSpeechAndStr(String text, String audioPath) throws Exception {
        JacobUtils.audioPath = audioPath;
        textToSpeech(text);
        FFmpegFrameGrabber grabber = FFmpegFrameGrabber.createDefault(JacobUtils.audioPath);
        grabber.start();
        long lengthInTime = grabber.getLengthInTime();
        System.out.println("秒数：" + lengthInTime / 1000 / 1000);
        SubtitleGenerator.generateSRT(text,audioPath + new Date().getTime(),lengthInTime / 1000 / 1000);
    }
}